package com.yanq.liancar.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import com.vondear.rxtool.RxConstTool;
import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.bean.BaseBean;
import com.yanq.liancar.bean.VerificationCodeBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.CountDownTimerUtils;
import com.yanq.liancar.util.LogUtils;
import com.yanq.liancar.util.Utils;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RegesterActivity extends AppCompatActivity implements View.OnClickListener {

    @BindView(R.id.back_btn)
    ImageView backBtn;
    @BindView(R.id.gang)
    TextView gang;
    @BindView(R.id.phone_num)
    EditText phoneNum;
    @BindView(R.id.sms)
    EditText sms;
    @BindView(R.id.register_pwd)
    EditText registerPwd;
    @BindView(R.id.sure_register)
    TextView sureRegister;
    @BindView(R.id.go_login)
    TextView goLogin;
    @BindView(R.id.btn_get_yzm)
    Button btn_get_yzm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Utils.setStatusBar(this, false, false);
        setContentView(R.layout.activity_regester);
        ButterKnife.bind(this);
        initViewsAndEvents();
        findViewById(R.id.go_login).setOnClickListener(this);
        findViewById(R.id.sure_register).setOnClickListener(this);
        findViewById(R.id.back_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        btn_get_yzm.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sure_register:
                initOkGo();
                break;
            case R.id.go_login:
                startActivity(new Intent(RegesterActivity.this, LoginActivity.class));
                finish();
                break;
            case R.id.btn_get_yzm:
                if (Utils.isMobile(phoneNum.getText().toString().trim())){
                    getRandomNum();
                }else {
                    RxToast.error("请输入正确电话号码");
                }
                break;
        }
    }

    private void initOkGo() {
        OkGo.<String>post(Config.s + "llc/register")
                .tag(this)
                .params("tel", phoneNum.getText().toString().trim())
                .params("password", registerPwd.getText().toString().trim())
                .params("code", sms.getText().toString().trim())
                .params("roleId", "1")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        try {
                            JSONObject jsonObject=new JSONObject(response.body());
                            if (jsonObject.getBoolean("IsSuccess")) {
                                startActivity(new Intent(RegesterActivity.this, LoginActivity.class));
                                finish();
                                RxToast.success(jsonObject.getString("OutputMessage"));
                            } else {
                                RxToast.error(jsonObject.getString("OutputMessage"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                });
    }

    private void getRandomNum() {
        OkGo.<String>post(Config.s + "llc/getCode")
                .tag(this)
                .params("tel", phoneNum.getText().toString().trim())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson=new Gson();
                        VerificationCodeBean bean=gson.fromJson(response.body(),VerificationCodeBean.class);
                        if (bean.isIsSuccess()){
                            CountDownTimerUtils countDownTimerUtils = new CountDownTimerUtils(btn_get_yzm, 60000, 1000);
                            countDownTimerUtils.start();
                            RxToast.success(bean.getOutputMessage());
                        }else {
                         RxToast.error(bean.getOutputMessage());
                        }
                    }
                });
    }

    private void initViewsAndEvents() {
        phoneNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (Utils.isMobile(editable.toString())){
                    btn_get_yzm.setEnabled(true);
                }else {
                    btn_get_yzm.setEnabled(false);
                }
            }
        });
    }
}
